//
//  RechargeViewController.m
//  LoveAttention
//
//  Created by 鞠超 on 15/12/29.
//  Copyright © 2015年 JC. All rights reserved.
//

#import "RechargeViewController.h"

#import "Pingpp.h"
#import "MJRefresh.h"
#import "DataProvider.h"
#import "SVProgressHUD.h"
#import "UIImageView+WebCache.h"
@interface RechargeViewController () <UITextFieldDelegate>
//积分
@property (nonatomic, strong) UITextField * text_jifen;
//金额
@property (nonatomic, strong) UITextField * text_jine;
//支付宝
@property (nonatomic, strong) UIButton * btn_zhifu;
//微信
@property (nonatomic, strong) UIButton * btn_weixin;
@end

@implementation RechargeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navigationController];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - navigationController
- (void)p_navigationController
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.navigationItem.title = @"充值";
    UIButton * btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [btn setBackgroundImage:[UIImage imageNamed:@"返回"] forState:(UIControlStateNormal)];
        btn.frame = CGRectMake(10, 20, 25, 30);
    [btn addTarget:self action:@selector(leftBarButtonItemAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
}
//点击事件(左)
- (void)leftBarButtonItemAction:(UIBarButtonItem *)bar
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 布局
- (void)p_setupView
{
    UIView * view_1 = [[UIView alloc] initWithFrame:CGRectMake(0, 64 + 10, self.view.frame.size.width, 50)];
    view_1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view_1];
    
    UILabel * label_jifen = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 50, 40)];
    label_jifen.text = @"积分:";
    [view_1 addSubview:label_jifen];
    
    self.text_jifen = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label_jifen.frame) + 10, 5, 200, 40)];
    self.text_jifen.placeholder = @"积分100";
    self.text_jifen.keyboardType = UIKeyboardTypeDecimalPad;
    [view_1 addSubview:self.text_jifen];
    self.text_jifen.delegate = self;
    
    
    
    UIView * view_2 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(view_1.frame) + 5 , self.view.frame.size.width, 50)];
    view_2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view_2];
    
    UILabel * label_jine = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 50, 40)];
    label_jine.text = @"金额:";
    [view_2 addSubview:label_jine];
    
    self.text_jine = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label_jine.frame) + 10, 5, 200, 40)];
    self.text_jine.placeholder = @"金额100";
    self.text_jine.keyboardType = UIKeyboardTypeDecimalPad;
    [view_2 addSubview:self.text_jine];
    self.text_jine.delegate = self;
    
    
    
    UIView * view_3 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(view_2.frame) + 15, self.view.frame.size.width, 50)];
    view_3.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view_3];
    
    UIImageView * image_zhifu = [[UIImageView alloc] initWithFrame:CGRectMake(20, 12.5, 25, 25)];
    image_zhifu.image = [UIImage imageNamed:@"zhifubao@2x"];
    [view_3 addSubview:image_zhifu];
    UILabel * label_zhifu = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_zhifu.frame) + 10, 10, 50, 30)];
    label_zhifu.text = @"支付宝";
    label_zhifu.font = [UIFont systemFontOfSize:13];
    [view_3 addSubview:label_zhifu];
    
    self.btn_zhifu = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.btn_zhifu.frame = CGRectMake(self.view.frame.size.width - 40, 12.5, 25, 25);
//    self.btn_zhifu.layer.cornerRadius = 12.5;
//    self.btn_zhifu.layer.borderColor = [UIColor grayColor].CGColor;
//    self.btn_zhifu.layer.borderWidth = 1;
//    self.btn_zhifu.backgroundColor = [UIColor orangeColor];
    [self.btn_zhifu setBackgroundImage:[UIImage imageNamed:@"未选中"] forState:(UIControlStateNormal)];
    self.btn_zhifu.selected = 0;
    [view_3 addSubview:self.btn_zhifu];
    [self.btn_zhifu addTarget:self action:@selector(btn_zhifuAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    UIView * view_4 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(view_3.frame) + 5, self.view.frame.size.width, 50)];
    view_4.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view_4];
    
    UIImageView * image_weixin = [[UIImageView alloc] initWithFrame:CGRectMake(20, 12.5, 25, 25)];
    image_weixin.image = [UIImage imageNamed:@"weixin@2x"];
    [view_4 addSubview:image_weixin];
    UILabel * label_weixin = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_weixin.frame) + 10, 10, 50, 30)];
    label_weixin.text = @"微信";
    label_weixin.font = [UIFont systemFontOfSize:13];
    [view_4 addSubview:label_weixin];
    
    self.btn_weixin = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.btn_weixin.frame = CGRectMake(self.view.frame.size.width - 40, 12.5, 25, 25);
    [self.btn_weixin setBackgroundImage:[UIImage imageNamed:@"未选中"] forState:(UIControlStateNormal)];
    self.btn_weixin.selected = 0;
    [view_4 addSubview:self.btn_weixin];
    [self.btn_weixin addTarget:self action:@selector(btn_weixinAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    //btn确定
    UIButton * btn_ok = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_ok.frame = CGRectMake(20, CGRectGetMaxY(view_4.frame) + 40, self.view.frame.size.width - 40 , 40);
    [btn_ok setTitle:@"确定" forState:(UIControlStateNormal)];
    [btn_ok setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    btn_ok.layer.cornerRadius = 4;
    btn_ok.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn_ok];
    [btn_ok addTarget:self action:@selector(btn_okAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

#pragma mark - 支付和微信的
//支付
- (void)btn_zhifuAction:(UIButton *)sender
{
    if(sender.selected == 0)
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"选中"] forState:(UIControlStateNormal)];
        sender.selected = 1;
        self.btn_weixin.selected = 0;
        [self.btn_weixin setBackgroundImage:[UIImage imageNamed:@"未选中"] forState:(UIControlStateNormal)];
    }
    else
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"未选中"] forState:(UIControlStateNormal)];
        sender.selected = 0;
    }
}
//微信
- (void)btn_weixinAction:(UIButton *)sender
{
    if(sender.selected == 0)
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"选中"] forState:(UIControlStateNormal)];
        sender.selected = 1;
        self.btn_zhifu.selected = 0;
        [self.btn_zhifu setBackgroundImage:[UIImage imageNamed:@"未选中"] forState:(UIControlStateNormal)];
    }
    else
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"未选中"] forState:(UIControlStateNormal)];
        sender.selected = 0;
    }
}

#pragma mark - 确定
- (void)btn_okAction:(UIButton *)sender
{
    //    NSLog(@"走支付流程");
    if([self.text_jine.text length] == 0)
    {
        UIAlertView * alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入金额" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];
        [alert show];
    }
    else
    {
        if(self.btn_weixin.isSelected == 1)
        {
            DataProvider * dataprovider = [[DataProvider alloc] init];
            NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
            [dataprovider setDelegateObject:self setBackFunctionName:@"Recharge:"];
            
            [dataprovider RechargeWith:[defaults objectForKey:@"merchantid"] andchannel:@"wx" andamount:self.text_jine.text];
        }
        
        if(self.btn_zhifu.isSelected == 1)
        {
            DataProvider * dataprovider = [[DataProvider alloc] init];
            NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
            [dataprovider setDelegateObject:self setBackFunctionName:@"Recharge:"];
            
            [dataprovider RechargeWith:[defaults objectForKey:@"merchantid"] andchannel:@"alipay" andamount:self.text_jine.text];
        }

    }
    
}

-(void)Recharge:(id)dict{
    NSLog(@"%@",dict);
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    NSString* str_data = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    [Pingpp createPayment:str_data
           viewController:self
             appURLScheme:@"LoveAttentionforsell.zykj"
           withCompletion:^(NSString *result, PingppError *error) {
               if ([result isEqualToString:@"success"]) {
                   // 支付成功
                   [self.navigationController popViewControllerAnimated:YES];
                   [SVProgressHUD showSuccessWithStatus:@"支付成功~" maskType:SVProgressHUDMaskTypeBlack];
               } else {
                   // 支付失败或取消
                   NSLog(@"Error: code=%lu msg=%@", error.code, [error getMsg]);
                   [SVProgressHUD showErrorWithStatus:@"支付失败~" maskType:SVProgressHUDMaskTypeBlack];
               }
           }];
}


#pragma mark - textField代理
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.text_jifen resignFirstResponder];
    [self.text_jine resignFirstResponder];
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    
    self.text_jifen.text = textField.text;
    
    self.text_jine.text = textField.text;
    return YES;
}



@end
